package tt.com.warehousemanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.enums.AuditEnum;
import tt.com.common.enums.StockEnum;
import tt.com.common.model.YxCommonWarehouseStock;
import tt.com.common.model.YxCommonWarehouseStockRecord;
import tt.com.common.util.AuditUtil;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.mapper.WarehouseStockRecordMapper;
import tt.com.warehousemanager.service.WarehouseStockRecordService;
import tt.com.warehousemanager.service.WarehouseStockService;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author Czycka
 * @date 2022/8/21 17:27
 * @description 仓库记录实现类
 */
@Service
public class WarehouseStockRecordServiceImpl extends ServiceImpl<WarehouseStockRecordMapper, YxCommonWarehouseStockRecord> implements WarehouseStockRecordService {

    @Autowired
    WarehouseStockService stockService;

    @Override
    public Integer insertRecord(YxCommonWarehouseStockRecord record, String createUserId, StockEnum stockType) {
        // 设置创建时间
        record.setCreateTime(new Date());
        // 设置创建操作人id
        record.setCreateUserId(createUserId);
        // 设置操作类型
        record.setType(stockType);
        // 设置操作时间
        record.setInTime(new Date());
        // 设置审核状况
        record.setAuditStatus(AuditEnum.PENDING_REVIEW);
        return baseMapper.insert(record);
    }

    @Override
    public Boolean insertOfBatch(List<YxCommonWarehouseStockRecord> records, String createUserId, StockEnum stockType) {
        Date createTime = new Date();
        records.forEach(record -> {
            // 设置创建时间
            record.setCreateTime(createTime);
            // 设置创建操作人id
            record.setCreateUserId(createUserId);
            // 设置操作类型
            record.setType(stockType);
            // 设置审核状况
            record.setAuditStatus(AuditEnum.PENDING_REVIEW);
        });
        return this.saveBatch(records);
    }

    @Override
    public Integer deleteRecord(Long stockId, String deleteUserId) {
        YxCommonWarehouseStockRecord record = baseMapper.selectById(stockId);
        if (record == null) {
            // 若仓库记录不存在则返回 -1
            return -1;
        } else if (record.getDataStatus() == 1) {
            // 若仓库记录已被删除则返回 -2
            return -2;
        } else {
            // 设置删除时间
            record.setDeleteTime(new Date());
            // 设置删除操作人id
            record.setDeleteUserId(deleteUserId);
            baseMapper.updateById(record);
            return baseMapper.deleteById(record.getId());
        }
    }

    @Override
    public Integer updateRecord(YxCommonWarehouseStockRecord stock, String updateUserId) {
        // 设置修改时间
        stock.setDeleteTime(new Date());
        // 设置修改操作人id
        stock.setDeleteUserId(updateUserId);
        return baseMapper.updateById(stock);
    }

    @Override
    public PageUtil showStockRecordByPage(PageUtil pageUtil) {
        Page<YxCommonWarehouseStockRecord> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<YxCommonWarehouseStockRecord> recordPage = baseMapper.selectPage(page, null);
        pageUtil.setTotalSize((int) recordPage.getTotal());
        pageUtil.setData(recordPage);
        return pageUtil;
    }

    @Override
    public Integer auditing(AuditUtil auditUtil) {
        // 根据工具类里的id 获取记录对象
        YxCommonWarehouseStockRecord record = baseMapper.selectById(auditUtil.getAuditorId());
        if (record == null) {
            return -2;
        }
        if (record.getAuditStatus() == AuditEnum.EXAMINATION_PASSED) {
            // 若已审核通过 则直接返回 -1
            return -1;
        }

        // 设置审核状态
        record.setAuditStatus(auditUtil.getState());
        // 设置审核人id
        record.setAuditUserId(auditUtil.getRecordId());
        // 设置审核时间
        record.setAuditTime(new Date());
        // 修改仓库记录数据
        baseMapper.updateById(record);
        // 入库
        YxCommonWarehouseStock stock = new YxCommonWarehouseStock();
        // 设置仓库id
        stock.setWarehouseId(record.getWarehouseId());
        // 设置物品id
        stock.setGoodsId(record.getGoodsId());
        // 设置物品数量
        stock.setCount(record.getCount());
        StockEnum type = record.getType();
        if (type == StockEnum.IN_RETURN || type == StockEnum.IN_NORMAL || type == StockEnum.IN_MOBILIZE) {
            // 入库
            return stockService.inStock(record, auditUtil.getAuditorId());
        } else {
            // 出库
            return stockService.outStock(record, auditUtil.getAuditorId());
        }
    }

    @Override
    public Boolean auditOfBatch(List<AuditUtil> auditUtils) {

        return null;
    }

    @Override
    public BigDecimal showCostByToday() {
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 开始时间当天零点
        calendar.add(Calendar.DATE,-1);
        // 生成开始时间
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }

    @Override
    public BigDecimal showCostByWeek() {
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 生成开始时间
        Date endTime = calendar.getTime();
        // 开始时间当周第一天
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }

    @Override
    public BigDecimal showCostByMonth(){
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 生成开始时间
        Date endTime = calendar.getTime();
        // 开始时间当月第一天
        calendar.set(Calendar.DAY_OF_MONTH,1);
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }

    @Override
    public BigDecimal showCostByYear() {
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 开始时间当年第一天
        calendar.set(Calendar.DAY_OF_YEAR,1);
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }

    private void removeHourMinuteSecond(GregorianCalendar calendar) {
        // 设置 时
        calendar.set(Calendar.HOUR_OF_DAY,0);
        // 设置 分
        calendar.set(Calendar.MINUTE,0);
        // 设置 秒
        calendar.set(Calendar.SECOND,0);
        // 设置 毫秒
        calendar.set(Calendar.MILLISECOND,0);
    }
}
